//
//  ZSHelper.m
//  DigitalWallet
//
//  Created by Apple on 2018/2/26.
//  Copyright © 2018年 ZDYA. All rights reserved.
//

#import "ZSHelper.h"


@interface ZSHelper()
//@property (strong,nonatomic) QMUIPopupContainerView *popupView1;
@property (copy, nonatomic) NSString *showText;
@end

@implementation ZSHelper
+ (void)showLabelInWindowWithText:(NSString *)text;
{
    
    UIView *maskView = [[UIView alloc] initWithFrame:(CGRectMake(0, 0, size_width, size_height))];
    CGRect tempRect = [text  boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width-32, 2000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]}context:nil];
    CGFloat height = tempRect.size.height;
    
    UILabel *showlabel = [[UILabel alloc] initWithFrame:(CGRectMake(16, size_height-64-49-20, size_width-32, height+10))];
    showlabel.font = [UIFont systemFontOfSize:14];
    showlabel.text = text;
    showlabel.textColor = [UIColor whiteColor];
    showlabel.numberOfLines = 0;
    showlabel.lineBreakMode = NSLineBreakByWordWrapping;
    showlabel.textAlignment = NSTextAlignmentCenter;
    showlabel.backgroundColor = [UIColor blackColor];
    showlabel.layer.cornerRadius = 5;
    showlabel.layer.masksToBounds = YES;
    
    [maskView addSubview:showlabel];
    maskView.backgroundColor = [UIColor clearColor];
    // [UIColor colorWithWhite:0.6 alpha:0.7];
    [maskView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideMask:)]];
    

    [[[UIApplication sharedApplication] keyWindow]  addSubview:maskView];
  
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [maskView removeAllSubviews];
        [maskView removeFromSuperview];
      
    });
}

+ (void)hideMask:(UIGestureRecognizer *)ges{
    [ges.view removeFromSuperview];
}

+ (UITapGestureRecognizer *)addShowActionToLabel:(UILabel *)label;{
    return [ZSHelper addTapGesToUIView:label text:nil];
}

+ (void)ZS_addShowActionToLabel:(UILabel *)label;{
    label.userInteractionEnabled = YES;
    [label addGestureRecognizer: [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGes:)] ];
}

+ (void)tapGes:(UITapGestureRecognizer *)ges{
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
    UILabel *label = (UILabel *)ges.view;
    NSLog(@"%@", label.text);
    [ZSHelper showLabelInWindowWithText:label.text];
}

+ (UITapGestureRecognizer *)addTapGesToUIView:(UIView *)sender text:(NSString *)text{
    UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapAction:)];
    if ([sender isKindOfClass:[UILabel class]]) {
        UILabel *label = (UILabel *)sender;
        label.userInteractionEnabled = YES;
        [label addGestureRecognizer: tapGes];
        //_showText = text;
        return tapGes;
    }
    sender.userInteractionEnabled = YES;
    
    [sender addGestureRecognizer:tapGes ];
    //_showText = text;
    return tapGes;
}

//+ (void)handleTapAction:(UIGestureRecognizer *)ges{
//    [[UIApplication sharedApplication].keyWindow endEditing:YES];
//    UIView *gesView = ges.view;
//    /*if (_showText.length==0) {
//        _showText = @"默认内容";
//    }
//    self.popupView1 = [[QMUIPopupContainerView alloc] init];
//    self.popupView1.automaticallyHidesWhenUserTap = YES;
//    self.popupView1.maskViewBackgroundColor = UIColorClear;// 使用方法 2 并且打开了 automaticallyHidesWhenUserTap 的情况下，可以修改背景遮罩的颜色
//    
//    //self.popupView1.imageView.image = [[UIImageMake(@"icon_emotion") qmui_imageWithScaleToSize:CGSizeMake(24, 24) contentMode:UIViewContentModeScaleToFill] qmui_imageWithTintColor:[QDThemeManager sharedInstance].currentTheme.themeTintColor];
//    self.popupView1.textLabel.text = _showText;
//    //self.popupView1.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 8);
//    self.popupView1.didHideBlock = ^(BOOL hidesByUserTap) {
//        NSLog(@"ddd");
//    };
//    // 使用方法 2
//    [self.popupView1 layoutWithTargetView:gesView];
//    [self.popupView1 showWithAnimated:YES];*/
//    
//    NSString *text = @"默认";
//    if ([gesView isKindOfClass:[UILabel class]]) {
//        text = [(UILabel *)gesView text];
//    }else if ([gesView isKindOfClass:[UITextView class]]){
//        text = [(UITextView *)gesView text];
//    }else if ([gesView isKindOfClass:[UITextField class]]){
//        text = [(UITextField *)gesView text];
//    }
//    
//     QMUIPopupContainerView *popupView1 = [[QMUIPopupContainerView alloc] init];
//     popupView1.automaticallyHidesWhenUserTap = YES;
//    popupView1.maskViewBackgroundColor = UIColorClear;// 使用方法 2 并且打开了 automaticallyHidesWhenUserTap 的情况下，可以修改背景遮罩的颜色
//    
//    //self.popupView1.imageView.image = [[UIImageMake(@"icon_emotion") qmui_imageWithScaleToSize:CGSizeMake(24, 24) contentMode:UIViewContentModeScaleToFill] qmui_imageWithTintColor:[QDThemeManager sharedInstance].currentTheme.themeTintColor];
//    popupView1.textLabel.text = text;
//    
//    //self.popupView1.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 8);
//    popupView1.didHideBlock = ^(BOOL hidesByUserTap) {
//     NSLog(@"ddd");
//     };
//     // 使用方法 2
//     [popupView1 layoutWithTargetView:gesView];
//     [popupView1 showWithAnimated:YES];
//}
//+ (QMUITips *)showError:(NSString *)text inView:(UIView *)view hideAfterDelay:(NSTimeInterval)delay;{
//    
//    return [QMUITips showError:text inView:view hideAfterDelay:delay];
//}
//+ (QMUITips *)showSucceed:(NSString *)text inView:(UIView *)view hideAfterDelay:(NSTimeInterval)delay{
//    
//    return [QMUITips showSucceed:text inView:view hideAfterDelay:delay];
//}
//+ (QMUITips *)showInfo:(NSString *)text inView:(UIView *)view hideAfterDelay:(NSTimeInterval)delay;{
//    return [QMUITips showInfo:text inView:view hideAfterDelay:delay];
//}
//+ (QMUITips *)showLoading:(NSString *)text inView:(UIView *)view;{
//    return [QMUITips showLoading:text inView:view];
//}
//+ (BOOL)hideAllToastInView:(UIView *)view animated:(BOOL)animated {
//    return [QMUITips hideAllToastInView:view animated:YES];
//}
//约分
+(NSString *)ZS_reductionOfAFractionWitSon:(NSUInteger)son mother:(NSUInteger)mother;
{
    NSUInteger min;
    min = son < mother ?son :mother;
    while (1)
    {
        if (son % min ==0 &&mother % min ==0 )
        {
            break;
        }
        min = min - 1;
    }
    NSLog(@"约分:%lud/%lu", son/min, mother/min);
    return [NSString stringWithFormat:@"%lu:%lu", son/min,mother/min];
}
+(void)alertWithTitle:(NSString *)title message:(NSString *)message controller:(UIViewController *)controller;{
    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:title  message:message preferredStyle:(UIAlertControllerStyleAlert)];
    [alertC addAction:[UIAlertAction actionWithTitle:@"好的" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
        
    }]];
    [controller presentViewController:alertC animated:YES completion:^{
        
    }];
}
+(void)alertWithTitle:(NSString *)title message:(NSString *)message controller:(UIViewController *)controller okAction:(void(^)(void)) okAction;{
    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:title  message:message preferredStyle:(UIAlertControllerStyleAlert)];
    [alertC addAction:[UIAlertAction actionWithTitle:@"好的" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
        okAction();
    }]];
    [controller presentViewController:alertC animated:YES completion:^{ }];
}
// 规范格式 去掉多位数的开头0或加上小数点左边没有的0
+(NSString *)checkNumberFormat:(NSString *)sourceString;
{
    // 规范格式 去掉多位数的开头0或加上小数点左边没有的0
    if ([sourceString containsString:@"."]) {
        NSArray *arr = [sourceString componentsSeparatedByString:@"."];
        NSString *leftStr = arr[0];
        NSString *rightStr = arr[1];
        if (leftStr.length==0) {
            sourceString = [@"0" stringByAppendingString:sourceString];
            
        }else if(leftStr.length>1){ //
            while ([leftStr hasPrefix:@"0"] && leftStr.length>1) {
                leftStr = [leftStr stringByReplacingCharactersInRange:(NSMakeRange(0, 1)) withString:@""];
            }
            sourceString = [leftStr stringByAppendingFormat:@".%@", rightStr];
        }else{ //
            
        }
    }
    return sourceString;
}
@end
